﻿using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace TFSBugTracking
{
    /// <summary>
    /// Data Container Class used to store a mapping between a software product where bugs need to be filed
    /// and a tfs project / area / iteration tupel where the bug work item is actually  created and assigned to
    /// </summary>
    public class ProjectBugMap
    {
        /// <summary>
        /// Well-known Software product name to file a bug for
        /// </summary>
        public string ProductName { get; set; }
        /// <summary>
        /// Tfs Project Name for the bug work item to be created in
        /// </summary>
        public string TfsProjectName { get; set; }
        /// <summary>
        /// Tfs Area Path for the bug work item to be created in
        /// </summary>
        public string TfsAreaPath { get; set; }
        /// <summary>
        /// Tfs Iteration Path for the bug work item to be created in
        /// </summary>
        public string TfsIterationPath { get; set; }
        /// <summary>
        /// Tfs User Name assigned to the new bug work item
        /// </summary>
        public string TfsDefaultAssignTo { get; set; }
        /// <summary>
        /// Returns an example collection of project mappings
        /// </summary>
        /// <returns>example collection valid for OSS</returns>
        public static ProjectBugMap[] GetMappings()
        {
            var bugMappings = new ProjectBugMap[6];
            bugMappings[0] = new ProjectBugMap
                                 {
                                     ProductName = "Optoplex NG Q",
                                     TfsProjectName = "Optoplex NG",
                                     TfsAreaPath = "Optoplex NG\\Optoplex NG Q",
                                     TfsIterationPath = "Optoplex NG\\ONGQ",
                                     TfsDefaultAssignTo = "Sebastian Dau"
                                 };

            bugMappings[1] = new ProjectBugMap
                                 {
                                     ProductName = "Optoplex III P",
                                     TfsProjectName = "Optoplex NG",
                                     TfsAreaPath = "Optoplex NG\\Optoplex III P",
                                     TfsIterationPath = "Optoplex NG\\OIIIP",
                                     TfsDefaultAssignTo = "Ulrich Basler"
                                 };

            bugMappings[2] = new ProjectBugMap
                                 {
                                     ProductName = "CarlZeiss.Framework2",
                                     TfsProjectName = "CarlZeiss.Framework2",
                                     TfsAreaPath = "CarlZeiss.Framework2",
                                     TfsIterationPath = "CarlZeiss.Framework2",
                                     TfsDefaultAssignTo = "Sebastian Dau"
                                 };

            bugMappings[3] = new ProjectBugMap
                                 {
                                     ProductName = "Aspect+",
                                     TfsProjectName = "Inhouse",
                                     TfsAreaPath = "Inhouse\\Aspect+",
                                     TfsIterationPath = "Inhouse\\",
                                     TfsDefaultAssignTo = "Eckhard Stein (TFS2010\\Stein)"
                                 };

            bugMappings[4] = new ProjectBugMap
                                 {
                                     ProductName = "McsMmsProd",
                                     TfsProjectName = "Inhouse",
                                     TfsAreaPath = "Inhouse\\McsMmsProd",
                                     TfsIterationPath = "Inhouse\\",
                                     TfsDefaultAssignTo = "Eckhard Stein (TFS2010\\Stein)"
                                 };
            bugMappings[5] = new ProjectBugMap
                                 {
                                     ProductName = "Cora+",
                                     TfsProjectName = "Inhouse",
                                     TfsAreaPath = "Inhouse\\Cora+",
                                     TfsIterationPath = "Inhouse\\",
                                     TfsDefaultAssignTo = "Ulrich Basler"
                                 };
            return bugMappings;
        }

        /// <summary>
        /// returns a specific mapping collection taken from an xml file (de-serialization)
        /// </summary>
        /// <param name="path">the path to read the serialization data from</param>
        /// <returns>the custom mapping set</returns>
        /// <exception cref="FileNotFoundException">when the mapping file was not found</exception>
        public static ProjectBugMap[] GetMappings(string path)
        {  
            if (!File.Exists(path))
                throw new FileNotFoundException("TfsMapping.xml not found. Configure appropriate path in Web Project Settings!", path);

            using (XmlTextReader xr = new XmlTextReader(path))
            {
                XmlSerializer xs = new XmlSerializer(typeof (ProjectBugMap[]));
                return (ProjectBugMap[]) xs.Deserialize(xr);
            }
        }
    }
}